package com.wisemoresystem.seal.action_app.mac;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_seal_log;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.AuthService;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * .net调用获取印控仪,印章json
 */

@Controller("ixMacAction")
@Scope("prototype")
@RequestMapping("/ix/net")
public class MacAction extends ProjBaseAction {

	@Autowired
	private BankService bService;
	@Autowired
	private SealSetService sService;
	@Autowired
	private MacService mService;
	@Autowired
	private AuthService authService;
	@Autowired
	private UserService userService;

	/**
	 * 印章列表
	 * 
	 * @return
	 */
	@RequestMapping("/mac_info")
	@ResponseBody
	public String mac_info() {

		String ids = sService.selectSealUserAuthList(WebTools
				.getCurrentUserId());
		List<Map<String, Object>> list = mService.selectUserMac(ids);
		String info = "";
		if (ChkTools.isNotNull(list)) {
			info = JsonTools.toJson(list.get(0));
		}
		return info;
	}

	@RequestMapping("/bank_mac_info")
	@ResponseBody
	public String bank_mac_info(String code, String mac_model) {
		System.out.println(code + "---" + mac_model);
		List<Map<String, Object>> list = mService.chkUserMac(code);
		String info = "false";
		if (ChkTools.isNotNull(list)) {
			t_mac mac = oc.findObjById(list.get(0).get("_id").toString(),
					t_mac.class);
			mac.setMac_model(mac_model);
			oc.updateObj(mac);
			info = "true";
		}

		return info;
	}

	/**
	 * 授权
	 * 
	 * @param boeing
	 * @return
	 */
	@RequestMapping("/check_auth")
	@ResponseBody
	public String check_auth(String boeing) {
		String result = "false";

		List<String> ulist = authService.selectNotAuth();
		if (ChkTools.isNotNull(ulist)) {
			for (String user_id : ulist) {
				t_user user = oc.findObjById(user_id, t_user.class);
				user.setUser_level("非主管");
				oc.updateObj(user);
			}
		}
		if (authService.auth(boeing)) {
			result = "true";
		}

		return result;
	}

	@RequestMapping("/seal_log")
	@ResponseBody
	public String seal_log(@RequestBody String json) throws IOException,
			ParseException {
		System.err.println("===========>" + json);
		Map<String, Object> map = JsonTools.jsonStrToMap(json);
		if (ChkTools.isNull(map)) {
			return "false";
		}
		String reslut = null;
		String file_id_before = sService.file_id((String) map
				.get("file_id_before"));
		String file_id = sService.file_id((String) map.get("file_id"));
		String file_id_user = sService
				.file_id((String) map.get("file_id_user"));
		System.out.println(file_id_before + "---" + file_id + "---"
				+ file_id_user);

		if (!("false".equals(file_id_before)) && !("false".equals(file_id))
				&& !("false".equals(file_id_user))) {

			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Timestamp ts = new Timestamp(format.parse(
					map.get("seal_time").toString()).getTime());
			Timestamp ats = null;
			if (ChkTools.isNotNull(map.get("auth_time"))) {
				ats = new Timestamp(format.parse(
						map.get("auth_time").toString()).getTime());
			}

			t_seal_log seal_log = new t_seal_log();
			seal_log.setBank_id(WebTools.getCurrentBankId());
			seal_log.setUser_id(WebTools.getCurrentUserId());
			seal_log.setFile_id_before(file_id_before);
			seal_log.setFile_id(file_id);
			seal_log.setFile_id_user(file_id_user);
			seal_log.setSeal_code(map.get("seal_code").toString());
			seal_log.setSeal_type(map.get("seal_type").toString());
			seal_log.setSeal_modle_code(map.get("seal_modle_code").toString());
			seal_log.setMac_code(map.get("mac_code").toString());
			seal_log.setFlag(map.get("flag").toString());
			seal_log.setSeal_time(ts);
			if (ChkTools.isNotNull(ats)) {
				seal_log.setAuth_time(ats);
			}
			if (ChkTools.isNotNull(map.get("auth_user_id"))) {
				seal_log.setAuth_user_id(map.get("auth_user_id").toString());
			}
			if (ChkTools.isNotNull(map.get("boeing"))) {
				t_user users = (t_user) userService.chkboEing(
						map.get("boeing").toString()).get(0);
				;
				seal_log.setAuth_user_id(users.get_id());
			}
			oc.saveObj(seal_log);

			logT.logH("添加印章日志", t_seal_log.class, seal_log.get_id());
			reslut = "true";
		} else {
			reslut = "false";
		}
		return reslut;

	}

	/**
	 * 印章日志列表
	 * 
	 * @return
	 */
	@RequestMapping("/seal_log_list")
	@ResponseBody
	public String seal_log_list() {

		List<Map<String, Object>> list = sService.selectSealUserLogList();
		String info = JsonTools.toJson(list);

		return info;
	}

}
